<?php
namespace app\backend\org;

use \app\backend\model\Menu;
use \app\backend\model\Lists;
use \app\backend\model\Tab;
use \think\Db;

class Crumbs
{
    //id为前端请求的ID
    static public function crumbs($data,$id,$language,$type)
    {
        if($type=="menu"){
            //echo "<pre>";
            //print_r($data);exit;
            $p_id= [];
            foreach ($data as $k=>$v){
                if($v["menu_id"]==$id && $p_id !=0){
                    $p_id = array_merge($p_id,self::crumbs($data,$v["p_id"],$language,$type));
                    if($language=="cn"){
                        $p_id[]=array("id"=>$id,"data_id"=>$id,"p_id"=>$v["p_id"],"menu_name"=>$v["menu_name_cn"]);
                    }elseif ($language=="en"){
                        $p_id[]=array("id"=>$id,"data_id"=>$id,"p_id"=>$v["p_id"],"menu_name"=>$v["menu_name_en"]);
                    }
                }
            }
        }elseif ($type=="list"){
            if($language=="cn"){
                $list_title = "list_title_cn";
                $tab_name = "tab_name_cn";
                $menu_name = "menu_name_cn";
            }elseif($language=="en"){
                $list_title = "list_title_en";
                $tab_name = "tab_name_en";
                $menu_name = "menu_name_en";
            }
            $list_data = Lists::field("list_id,p_id,$list_title as list_title")->where(["list_id"=>$id,"delete_time"=>["exp","is null"]])->select();
           // var_dump($list_data[0]);exit;
            $tab_data = Tab::field("tab_id,p_id,$tab_name as tab_name")->where(["tab_id"=>$list_data[0]["p_id"],"delete_time"=>["exp","is null"]])->select();
           // var_dump($tab_data[0]["tab_id"]);exit;
            $menu_data = Menu::field("menu_id,p_id,$menu_name as menu_name")->where(["menu_id"=>$tab_data[0]["p_id"],"delete_time"=>["exp","is null"]])->select();
            //var_dump($menu_data[0]["menu_id"]);exit;
            $list_data[0]["data_id"] =$tab_data[0]["data_id"]=$menu_data[0]["data_id"] = $menu_data[0]["menu_id"];
            if($menu_data[0]["p_id"]!=0){
                $menu_data2 =  Menu::field("menu_id,p_id,$menu_name as menu_name")->where(["delete_time"=>["exp","is null"]])->select();
                $menu_p= self::_get_parent($menu_data2,$menu_data[0]["p_id"]);

                $p_id= array_merge($menu_p,[$menu_data[0],$tab_data[0],$list_data[0]]);
                //var_dump($p_id);exit;
            }else{
                $p_id= [$menu_data[0],$tab_data[0],$list_data[0]];
            }
            //echo "<pre>";
            //print_r($data);exit;
        }elseif ($type=="joinus"){
            if($language=="cn"){
                $title = "title_cn";
                $menu_name = "menu_name_cn";
            }elseif($language=="en"){
                $title = "title_en";
                $menu_name = "menu_name_en";
            }
            $joinus =  Db::query("select p_id, joinus_id, $title as title  from ls_joinus where `joinus_id`={$id} and `delete_time` is null");
            $menu_data = Menu::field("menu_id,p_id,$menu_name as menu_name")->where(["menu_id"=>$joinus[0]["p_id"],"delete_time"=>["exp","is null"]])->select();
            $p_id = [$menu_data[0],$joinus[0]];
        }
        return $p_id;
    }

    //递归
    static protected function _get_parent($data, $pid=0){
        $array = array();
        foreach ($data as $k => $v){
            if ($v['menu_id'] == $pid && $v['p_id'] != 0){
                //echo 111;exit;
                $v["data_id"] = $v["menu_id"];
                $array[] = $v;
                if ($v['p_id'] != 0) {
                    $array = array_merge(self::_get_parent($data,$v["p_id"]), $array);
                }
            }
        }
        return $array;
    }
}